import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Yin_JC
 * @PackageName:PACKAGE_NAME
 * @Class:DateTimeLearing
 * @Description:
 *
 * 1、System.currentTimeMillis()  常用
 * 2、util.Date   sql.Date  常用
 * 3、SimpleDateFormat 常用
 * 4、Calendar
 * 5、Instant
 * 6、LocalDateTime 常用
 * 7、DateTimeFormatter (可与LocalDateTime搭配）常用
 *
 * @date 2020/3/3 9:25
 */

public class DateTimeLearing {
    @Test
    public void test1(){
        long time = System.currentTimeMillis();
        System.out.println(time);
    }

    @Test
    public void test2(){
        Date date = new Date();
        System.out.println(date.getTime());
    }

    @Test
    public void test3(){
        java.sql.Date date = new java.sql.Date(1583199039569L);
        System.out.println(date);


        //将Date转化为sql.Date
        java.sql.Date date2 = new java.sql.Date(new Date().getTime());
        System.out.println(date2);
    }

    @Test
    public void test4() throws ParseException {
        /**
        * Description:
         *  常用格式化：SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        * @author: Yin
        * @date: 2020/3/3 9:47
        */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
        System.out.println(format);
        String str = "2019-9-9 11:33:34";
        Date date1 = sdf.parse(str);
        System.out.println(date1);

        Date d1 = new Date();
        System.out.println();
    }

    @Test
    public void test5(){
        Calendar calendar = Calendar.getInstance();
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        calendar.set(Calendar.DAY_OF_MONTH,16);
        Date date = calendar.getTime();
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = sdf.format(date);
        System.out.println(format);

    }
    
    @Test
    public void test6(){
        /**
        * Description: 常用类 LocalDate,LocalTime,LocalDateTime,常用LocalDateTime
        * @author: Yin
        * @date: 2020/3/3 10:09
        */
        LocalDate nowDate = LocalDate.now();
        LocalTime nowTime = LocalTime.now();
        LocalDateTime nowDateTime = LocalDateTime.now();

        System.out.println(nowDate);
        System.out.println(nowDateTime);
        System.out.println(nowTime);

        LocalDateTime dateTime = LocalDateTime.of(2020, 10, 11, 00, 00, 00);
        LocalDateTime localDateTime = dateTime.withDayOfMonth(2);
        System.out.println(localDateTime);
        System.out.println(dateTime);


    }

    @Test
    public void test7(){
        Instant ins = Instant.now();
        System.out.println(ins);
        Date date = new Date(ins.toEpochMilli());
        System.out.println( date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = sdf.format(ins.toEpochMilli());
        System.out.println(format);

    }

    @Test
    public void dateTimeFormatter(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format = formatter.format(now);
        System.out.println(format);
    }
}
